
package com.bbs.ptc.run;

import org.apache.log4j.Logger;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.bbs.ptc.handler.GlobalHandler;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class JfinalConfig extends JFinalConfig {
	private static Logger log = Logger.getLogger(JfinalConfig.class);
	private boolean isDev = isDevMode();
	
	private boolean isDevMode() {
		String osName = System.getProperty("os.name");// 操作系统的名称
		return osName.indexOf("Windows") != -1;
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: configConstant
	 * Description: 配置常量
	 * Created On: 2014年9月3日 上午11:18:23
	 * @author JiaYongChao
	 * <p>
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configConstant(com.jfinal.config.Constants)
	 */
	//@formatter:on
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		if (isDev) {
			loadPropertyFile("classes/db.properties");
		}
		log.info("configConstant 设置是否开发模式");
		me.setDevMode(true);
		log.info("configConstant 设置视图类型为Jsp");
		me.setViewType(ViewType.JSP);
		log.info("configConstant 视图error page设置");
		me.setError401View("/common/error/401.html");
		me.setError403View("/common/error/403.html");
		me.setError404View("/common/error/404.html");
		me.setError500View("/common/error/500.html");
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: configRoute
	 * Description:  配置路由
	 * Created On: 2014年9月3日 上午11:18:43
	 * @author JiaYongChao
	 * <p>
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configRoute(com.jfinal.config.Routes)
	 */
	//@formatter:on
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		log.info("configRoute 表扫描注册开始");
		me.add(new AutoBindRoutes());
		log.info("configRoute 表扫描注册结束");
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: configPlugin
	 * Description:  配置插件
	 * Created On: 2014年9月3日 上午11:18:57
	 * @author JiaYongChao
	 * <p>
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configPlugin(com.jfinal.config.Plugins)
	 */
	//@formatter:on
	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		// 配置Druid 数据库连接池插件
		DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
		dp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType(getProperty("dbType"));
		dp.addFilter(wall);
		me.add(dp);
		// 配置AutoTableBindPlugin插件
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(dp);
		atbp.setShowSql(true);
		log.info("EhCachePlugin EhCache缓存");
		me.add(new EhCachePlugin());
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: configInterceptor
	 * Description: 配置全局拦截器
	 * Created On: 2014年9月3日 上午11:19:01
	 * @author JiaYongChao
	 * <p>
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configInterceptor(com.jfinal.config.Interceptors)
	 */
	//@formatter:on
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		// 让 模版 可以使用session
		me.add(new SessionInViewInterceptor());
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: afterJFinalStart
	 * Description: 系统启动完成后执行
	 * Created On: 2014年9月3日 下午1:57:11
	 * @author JiaYongChao
	 * <p>
	 * @see com.jfinal.config.JFinalConfig#afterJFinalStart()
	 */
	//@formatter:on
	public void afterJFinalStart() {
		/*log.info("afterJFinalStart 缓存参数");
		new ThreadParamInit().start();

		log.info("afterJFinalStart 启动操作日志入库线程");
		ThreadSysLog.startSaveDBThread();

		log.info("afterJFinalStart 创建自动回复lucene索引");
		new DocKeyword().run(); 

		log.info("afterJFinalStart 系统负载");
		TimerResources.start();*/
	}
	
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: beforeJFinalStop
	 * Description:  系统关闭前调用
	 * Created On: 2014年9月3日 下午1:57:21
	 * @author JiaYongChao
	 * <p>
	 * @see com.jfinal.config.JFinalConfig#beforeJFinalStop()
	 */
	//@formatter:on
	public void beforeJFinalStop() {
		/*log.info("beforeJFinalStop 释放lucene索引资源");
		new DocKeyword().close();

		log.info("beforeJFinalStop 释放日志入库线程");
		ThreadSysLog.setThreadRun(false);

		log.info("beforeJFinalStop 释放系统负载抓取线程");
		TimerResources.stop();*/
	}
	//@formatter:off 
	/**(non-Javadoc)
	 * Title: configHandler
	 * Description: 配置处理器
	 * Created On: 2014年9月3日 上午11:19:03
	 * @author JiaYongChao
	 * <p>
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configHandler(com.jfinal.config.Handlers)
	 */
	//@formatter:on
	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		/*log.info("configHandler 全局配置处理器，主要是记录日志和request域值处理");
		me.add(new GlobalHandler());*/
		me.add(new DruidStatViewHandler("/druid"));
		me.add(new ContextPathHandler());
	}
	
}
